package com.sh.learn.uspcat.extjs4.controller;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sh.learn.uspcat.extjs4.model.User;

@Controller
public class IndexController {

    @RequestMapping(value = "/lesson/{view}", method = RequestMethod.GET)
	public String lesson(@PathVariable("view") String view) {
		return "lesson" + view;
	}
    
    @RequestMapping(value = "/lesson03/{view}", method = RequestMethod.GET)
    public void lesson03(@PathVariable("view") String view, HttpServletResponse response, PrintWriter out) {
    	String text = "";
    	if(view.equals("ajaxdataproxy")){
    		text = "{name: 'kk', age: 33, email: 'ddd@153.com'}";
    	}
    	response.setCharacterEncoding("UTF-8");
        out.write(text);
        out.flush();
        out.close();
	}
    
    @RequestMapping(value = "/lesson04/{view}", method = RequestMethod.GET)
    public void lesson04(@PathVariable("view") String view, HttpServletResponse response, PrintWriter out) {
    	String text = "";
    	if(view.equals("ajaxdataproxy")){
    		text = "{name: 'kk', age: 33, email: 'ddd@153.com'}";
    	}
    	response.setCharacterEncoding("UTF-8");
        out.write(text);
        out.flush();
        out.close();
	}
    
    @RequestMapping(value = "/lesson05/{view}", method = RequestMethod.GET)
    public void lesson05(@PathVariable("view") String view, HttpServletResponse response, PrintWriter out) {
    	String text = "";
    	if(view.equals("datareaderxml")){
    		text = "<users><user><id>1</id><name>u1</name></user></users>";
    	}else if(view.equals("datareaderarray")){
    		text = "[[1, 'u1']]";
    	}
    	response.setCharacterEncoding("UTF-8");
        out.write(text);
        out.flush();
        out.close();
	}
    
    // 配置jackson
    // JSON的输入"Content-Type:application/json"
    @RequestMapping(value = "/lesson05/jsonRequest")
    public void jsonRequest(@RequestBody User user){
    	System.out.println(user.getName());
    }
    
    // XML的输入 Content-Type:text/xml
    // 不写method = RequestMethod.xxx，表示都可以接收
    @RequestMapping(value = "/lesson05/xmlRequest")
    public void xmlRequest(@RequestBody DOMSource a) {
		try {
			StringWriter writer = new StringWriter();
			StreamResult result = new StreamResult(writer);
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			transformer.transform(a, result);
			System.out.println(writer.toString());
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
    }
    
    @RequestMapping(value = "/lesson06/{view}", method = RequestMethod.GET)
    public void lesson06(@PathVariable("view") String view, HttpServletResponse response, PrintWriter out) {
    	String text = "";
    	if(view.equals("ajaxdataproxy")){
    		text = "{name: 'kk', age: 33, email: 'ddd@153.com'}";
    	}
    	response.setCharacterEncoding("UTF-8");
        out.write(text);
        out.flush();
        out.close();
	}
    
    @RequestMapping(value = "/lesson08/{view}")
    public void lesson08(@RequestBody User user, @PathVariable("view") String view, HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws Exception{
    	String text = "";
    	if(view.equals("data1")){
    		text = "{name: 'kk', age: 33, email: 'ddd@153.com'}";
    		System.out.println(request.getParameter("id"));
    	}else if(view.equals("form")){
    		text = "{name: 'kk', age: 33, email: 'ddd@153.com'}";
    		System.out.println(request.getParameter("id"));
    		System.out.println(request.getParameter("name"));
    	}else if(view.equals("element")){
    		text = "2011";
    	}else if(view.equals("refresh")){
    		text = String.valueOf(Math.random());
    	}else if(view.equals("json")){
    		System.out.println(user.getName());
    	}
    	response.setCharacterEncoding("UTF-8");
        out.write(text);
        out.flush();
        out.close();
	}
    
    
}
